﻿@using eLoan.Models;
@model CollectionReconciliationEditModel

@{
    ViewBag.Title = "Edit Collection Reconciliation Report";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>Edit Collection Reconciliation Report</h2>
        <ol class="breadcrumb">
            <li>
                Reports
            </li>
            <li class="active">
                <strong>Edit Collection Reconciliation Report</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>


<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-8">
            <div class="ibox float-e-margins">
            @using (Html.BeginForm("CollectionReconciliationReportEdit", "Report", FormMethod.Post, new { id = "saveForm" }))
            {
                <fieldset>
                    <div class="ibox-content">
                        <table id="tbl" class="table table-bordered">
                            <tr>
                                <th>Date</th>
                                <td>
                                    @Html.ValueFor(m => m.CollectionReconciliationItem.Date, "{0:MMM/dd/yyyy}")
                                    @Html.HiddenFor(m => m.CollectionReconciliationItem.Date)
                                    @Html.HiddenFor(m => m.CollectionReconciliationItem.ID)
                                </td>
                            </tr>
                            <tr>
                                <th>Branch</th>
                                <td>
                                    @Html.DisplayFor(m => m.BranchName)
                                    @Html.HiddenFor(m => m.CollectionReconciliationItem.BranchCode)
                                </td>
                            </tr>
                            <tr>
                                <th>Agent</th>
                                <td>
                                    @Html.DisplayFor(m => m.AgentName)
                                    @Html.HiddenFor(m => m.CollectionReconciliationItem.AgentID)
                                </td>
                            </tr>
                            <tr>
                                <th>Total Collection Per Record</th>
                                <td>
                                    @Html.ValueFor(m => m.CollectionReconciliationItem.TotalCollectionPerRecord, "{0:#,###,##0.00}")
                                    @Html.HiddenFor(m => m.CollectionReconciliationItem.TotalCollectionPerRecord, new { @id = "hdnTotalPerRecord" })
                                </td>
                            </tr>
                            <tr>
                                <th>Cash Deposited</th>
                                <td>
                                    @Html.TextBoxFor(m => m.CollectionReconciliationItem.CashDeposited, "{0:#,###,##0.00}", new { @id = "cashDeposited", @class = "single-line", @onchange = "FormatMe(this); GetTotal()" })
                                </td>
                            </tr>
                            <tr>
                                <th>Deducted To Cheque - Advance Payment</th>
                                <td>
                                    @Html.TextBoxFor(m => m.CollectionReconciliationItem.DeductedToChequeAdvancePayment, "{0:#,###,##0.00}", new { @id = "deductedToChequeAdvancePayment", @class = "single-line", @onchange = "FormatMe(this); GetTotal()" })
                                </td>
                            </tr>
                            <tr>
                                <th>Deducted To Cheque - Savings</th>
                                <td>
                                    @Html.TextBoxFor(m => m.CollectionReconciliationItem.DeductedToChequeSavings, "{0:#,###,##0.00}", new { @id = "deductedToChequeSavings", @class = "single-line", @onchange = "FormatMe(this); GetTotal()" })
                                </td>
                            </tr>
                            <tr>
                                <th>Used For Loan Release</th>
                                <td>
                                    @Html.TextBoxFor(m => m.CollectionReconciliationItem.UsedForLoanRelease, "{0:#,###,##0.00}", new { @id = "usedForLoanRelease", @class = "single-line", @onchange = "FormatMe(this); GetTotal()" })
                                </td>
                            </tr>
                            <tr>
                                <th>Used In Withdrawal Of Savings</th>
                                <td>
                                    @Html.TextBoxFor(m => m.CollectionReconciliationItem.UsedInWithdrawalOfSavings, "{0:#,###,##0.00}", new { @id = "usedInWithdrawalOfSavings", @class = "single-line", @onchange = "FormatMe(this); GetTotal()" })
                                </td>
                            </tr>
                            <tr>
                                <th>Others</th>
                                <td>
                                    @Html.TextBoxFor(m => m.CollectionReconciliationItem.Others, "{0:#,###,##0.00}", new { @id= "others", @class = "single-line", @onchange = "FormatMe(this); GetTotal()" })
                                </td>
                            </tr>
                            <tr>
                                <th>Remarks</th>
                                <td>
                                    @Html.TextBoxFor(m => m.CollectionReconciliationItem.Remarks, new { @class = "single-line" })
                                </td>
                            </tr>
                            <tr>
                                <th>Total</th>
                                <td>
                                    <label id="lblTotal">@Html.ValueFor(m => m.CollectionReconciliationItem.Total, "{0:#,###,##0.00}")</label>
                                    @Html.HiddenFor(m => m.CollectionReconciliationItem.Total, new { @id = "hdnTotal"})
                                </td>
                            </tr>
                            <tr>
                                <th>Difference</th>
                                <td>
                                    <label id="lblDifference">@Html.ValueFor(m => m.CollectionReconciliationItem.Difference, "{0:#,###,##0.00}")</label>
                                    @Html.HiddenFor(m => m.CollectionReconciliationItem.Difference, new { @id = "hdnDifference" })
                                </td>
                            </tr>
                        </table>
                           
                           <button type="submit" id="btnSubmit" class="btn btn-primary" name="command" value=Save>Save</button> 
                           <button type="submit" id="btnCancel" class="btn btn-primary cancel" name="command" value="Cancel">Cancel</button>

                    </div>

                </fieldset>
            }
            </div>

        </div>
    </div>
</div>

@section Styles {
    @Styles.Render("~/Content/plugins/dataTables/dataTablesStyles")
    @Styles.Render("~/plugins/dataPickerStyles")
    
}

@section Scripts {
@Scripts.Render("~/plugins/dataTables")
@Scripts.Render("~/plugins/dataPicker")

<script type="text/javascript">

    $(document).ready(function () {

        $(window).keydown(function (event) {
            if (event.keyCode == 13) {
                Refresh();
                return false;
            }
        });

    });

    function FormatMe(me) {
        var val = $(me).val().replace(',', '').replace(',', '').replace(',', '');

        if (isNaN(parseFloat(val)))
            val = 0.00;

        var decimalFormat = parseFloat(val).toFixed(2);
        var commaFormat = FormatNumber(decimalFormat);

        $(me).val(commaFormat);
    }

    function FormatNumber(nStr) {
        nStr += '';
        var x = nStr.split('.');
        var x1 = x[0];
        var x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return x1 + x2;
    }

    function GetTotal() {
        var total = 0;
        total = parseFloat($('#cashDeposited').val().replace(',', '').replace(',', '')) +
                parseFloat($('#deductedToChequeAdvancePayment').val().replace(',', '').replace(',', '')) +
                parseFloat($('#deductedToChequeSavings').val().replace(',', '').replace(',', '')) +
                parseFloat($('#usedForLoanRelease').val().replace(',', '').replace(',', '')) +
                parseFloat($('#usedInWithdrawalOfSavings').val().replace(',', '').replace(',', '')) +
                parseFloat($('#others').val());

        $('#lblTotal').text(FormatNumber(total.toFixed(2)));
        $('#hdnTotal').val(FormatNumber(total.toFixed(2)));

        var totalPerRecord = parseFloat($('#hdnTotalPerRecord').val().replace(',', '').replace(',', ''))
        var difference = total - totalPerRecord;

        $('#lblDifference').text(FormatNumber(difference.toFixed(2)));
        $('#hdnDifference').val(FormatNumber(difference.toFixed(2)));

    }

</script>

}
